﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IssueTrackerTest
{
    /// <summary>
    /// Summary description for IssueProcessTest
    /// </summary>
    [TestClass]
    public class IssueProcessTest
    {
        public IssueProcessTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestProcessIssue()
        {
            IssueProcessService.IssueProcessClient client = new IssueTrackerTest.IssueProcessService.IssueProcessClient();

            IssueProcessService.Issue issue = new IssueTrackerTest.IssueProcessService.Issue();

            IssueTrackerService.IssueTrackerServiceClient itsClient = new IssueTrackerTest.IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                
                itsClient.Open();

                List<IssueTrackerService.Project> projects = itsClient.ListProjects().ToList<IssueTrackerService.Project>();

                int projectId = projects.First().Id;
               
                client.Open();

                issue.Id = System.Guid.NewGuid();
                issue.Description = "test";
                issue.LongDescription = "long test";
                issue.ProjectId = projectId;
                issue.Title = "test";
                issue.CreatorId = "test";
                issue.CreationDate = DateTime.Today.Date;
                

                client.ProcessIssue(issue);

                
            }
            catch (Exception ex)
            { 
                Assert.Fail(ex.Message);
            }
            finally
            {
                itsClient.Close();

                client.Close();
            }
        }

        [TestMethod]
        public void SimulateSimpleFlow()
        {
            System.Guid workflowId = System.Guid.Empty;

            //approve - validate

            IssueTrackerService.IssueTrackerServiceClient itsClient = new IssueTrackerService.IssueTrackerServiceClient();

            try
            {
                List<IssueTrackerService.Issue> issues = itsClient.ListIssues().ToList<IssueTrackerService.Issue>();

                IssueTrackerService.Issue currentIssue;

                foreach (IssueTrackerService.Issue issue in issues)
                {
                    if (issue.WfWorkflowId != null)
                    {
                        if (issue.CurrentStateName == "Pending Approval")
                        {
                            workflowId = (System.Guid)issue.WfWorkflowId;
                            currentIssue = issue;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (itsClient.State == System.ServiceModel.CommunicationState.Opened)
                    itsClient.Close();
            }

            if (workflowId != System.Guid.Empty)
            {

                //hardcode first

                workflowId = new Guid("3bc63f14-bb40-46fc-9c57-1d99ecabffd5");
                IssueProcessService.IssueProcessClient client = new IssueTrackerTest.IssueProcessService.IssueProcessClient();

                try
                {
                    System.ServiceModel.Channels.IContextManager cm = client.InnerChannel.GetProperty<System.ServiceModel.Channels.IContextManager>();

                    IDictionary<string, string> workflowContext = IssueTracker.Helper.WorkflowServiceContextHelper.CreateContextFromGuid(workflowId);
                    cm.SetContext(workflowContext);

                    client.IssueValidated();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (client.State == System.ServiceModel.CommunicationState.Opened)
                        client.Close();
                }

            }
            
            //complete  - fixed

            //accept - approved


        }

        [TestMethod]
        public void IssueValidated()
        {

        }

        [TestMethod]
        public void IssueRejected()
        {

        }

        [TestMethod]
        public void IssueFixed()
        {

        }


        [TestMethod]
        public void SolutionApproved()
        {

        }


        [TestMethod]
        public void SolutionRejected()
        {

        }
    }
}
